package com.kintreda.ieco_server.module.api.web;

import com.kintreda.ieco_server.bean.account.User;
import com.kintreda.ieco_server.bean.attention.Attention;
import com.kintreda.ieco_server.core.repository.IBaseRepository;
import com.kintreda.ieco_server.core.web.BaseController;
import com.kintreda.ieco_server.core.web.R;
import com.kintreda.ieco_server.module.account.service.IUserService;
import com.kintreda.ieco_server.module.attention.service.IAttentionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @Created by IntelliJ IDEA
 * @Project_Name ieco_server
 * @Package com.kintreda.ieco_server.module.attention.web
 * @Author ZhongYao_H
 * @Date 2019/4/29:14:54
 * @Year 2019 Month 04 Day 29
 * @Date_zh 2019 年 四月 星期一
 */
@Controller
@RequestMapping("/api/attention")
public class ApiAttentionController extends BaseController {
    @Autowired
    private IAttentionService attentionService;
    @Autowired
    private IBaseRepository<Attention,String> attentionStringIBaseRepository;
    @Autowired
    private IUserService userService;


    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public Object add(String orgId,String openid ){


        //验证登录用户逻辑
        Attention attention =  new Attention();
        attention.setOrgId(orgId);
        String userId = "";

        try {
             userId = userService.findUserByOpenId(openid).getId();
            attention.setUserId(userId);
            attention.setOpenId(openid);
        }catch (Exception e){
            return R.error("请先登录");
        }
        if (attentionService.isFocus(userId,orgId)){
            //欺骗用户已关注
            return R.ok("成功关注");
        }

        String save = attentionService.save(attention).toString();
        if (StringUtils.isNotBlank(save)){
            return R.ok("成功关注");
        }else {
            return R.error("操作失败!稍后重试");
        }

    }



    @RequestMapping(value = "/del",method = RequestMethod.POST)
    @ResponseBody
    public Object del(String orgId,String openid ){

        //验证登录获取用户id
        String userId = "";
        try {
             userId = userService.findUserByOpenId(openid).getId();
        }catch (Exception e){
            return R.error("请先登录");
        }
        int i = attentionStringIBaseRepository.excuteBySql(" delete from attention where userId = ? and orgId = ? ", new Object[]{userId, orgId});
        if (i>0){
            return R.ok("取关成功");
        }else {
            return R.error("操作失败!稍后重试");
        }

    }

    @ResponseBody
    @RequestMapping(value = "/getlist",method = RequestMethod.POST)
    public Object getListUser(String orgId){
        List<Map<String, Object>> list = attentionStringIBaseRepository.findListBySql("select openId from attention where orgId =  ?", new Object[]{orgId});
        for (Map<String, Object> map : list) {
            User user = userService.findUserByOpenId(map.get("openId").toString());
            map.put("name",user.getName());
        }
        return list;
    }




}
